package com.levenx.web.rest;

import com.levenx.service.ReportService;
import com.levenx.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping
public class ReportController<T> {

    /**报表服务公共集合*/
    @Autowired
    private Map<String, ReportService<T>> reportServiceMap;



    /**
     * 报表查询
     * @param param
     * @param pn
     * @param ps
     * @return
     */
    @PostMapping("/report/{type}/search/{pn}/{ps}")
    public Result<?> findAttendance(@RequestBody Object param,
                                       @PathVariable String type,
                                       @PathVariable Integer pn,
                                       @PathVariable Integer ps){
        //拿出服务类
        ReportService reportService = reportServiceMap.get(type+"Report");
        return reportService.search(param,pn,ps);
    }



    /**
     * 报表查询总数
     * @param param
     * @return
     */
    @PostMapping("/report/{type}/count")
    public Result<?> reportCount(@RequestBody Object param,
                                @PathVariable String type) throws ClassNotFoundException, NoSuchMethodException {
        //拿出服务类
        ReportService reportService = reportServiceMap.get(type+"Report");
        return reportService.count(param);
    }


    /**
     * 报表导出
     * @param param
     * @param response
     */
    @PostMapping("/report/{type}/export")
    public void exportExcel(@RequestBody Object param,
                            @PathVariable String type,
                            HttpServletResponse response) {
        //拿出服务类
        ReportService reportService = reportServiceMap.get(type+"Report");
        reportService.export(param,response);
    }


    /**
     * 图表
     */
    @PostMapping("/report/chart/{type}")
    public Result<?> findChartData(@RequestBody Object param,
                                  @PathVariable String type){
        //拿出服务类
        ReportService reportService = reportServiceMap.get(type+"Report");
        return reportService.chart(param);
    }

}
